package com.bingxue.edu.management.education.controller;

import com.bingxue.edu.common.enums.OperateSourceEnum;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.query.ClassLessonQuery;
import com.bingxue.edu.management.education.model.query.LessonStudentQuery;
import com.bingxue.edu.management.education.model.req.ClassScheduleReq;
import com.bingxue.edu.management.education.model.req.LessonReq;
import com.bingxue.edu.management.education.model.req.LessonSignInReq;
import com.bingxue.edu.management.education.model.resp.ClassLessonResp;
import com.bingxue.edu.management.education.model.resp.ClassPreScheduleResp;
import com.bingxue.edu.management.education.model.resp.LessonStudentDetailResp;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.management.education.service.LessonStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * 班级课次
 * @author zq
 * @since 2024/08/26
 */
@Tag(name = "班级课次")
@RestController
@RequestMapping("/education/class/lesson")
@RequiredArgsConstructor
public class ClassLessonController {

    private final LessonService lessonService;
    private final LessonStudentService lessonStudentService;

    @Operation(summary = "排课信息-根据班级id查询排课信息")
    @ApiResource("edu:class:read")
    @GetMapping("/list")
    public Response<List<ClassLessonResp>> getClassLessonList(ClassLessonQuery query) {
        return Response.success(lessonService.getClassLessonList(query));
    }

    @Operation(summary = "更新课次信息")
    @ApiResource("edu:class:write")
    @PostMapping("/update/{id}")
    public Response<Void> updateClassLesson(@RequestBody @Validated LessonReq req,@PathVariable String id) {
        lessonService.updateClassLesson(req, id);
        return Response.success();
    }

    @Operation(summary = "获取课次学生列表")
    @ApiResource("edu:class:read")
    @GetMapping("/student/list")
    public Response<List<LessonStudentDetailResp>> getLessonStudentList(LessonStudentQuery query) {
        return Response.success(lessonStudentService.getLessonStudentList(query));
    }

    @Operation(summary = "点名")
    @ApiResource("edu:class:signIn")
    @PostMapping("/signIn")
    public Response<Void> signIn(@RequestBody @Validated LessonSignInReq req) {
        req.setOperateSource(OperateSourceEnum.BACKEND);
        lessonService.signIn(req);
        return Response.success();
    }

    @Operation(summary = "撤销点名")
    @ApiResource("edu:class:signIn")
    @PostMapping("/unSignIn")
    public Response<Void> unSignIn(@RequestBody @Validated LessonSignInReq req) {
        lessonService.unSignIn(req);
        return Response.success();
    }

    @Operation(summary = "班级预排课")
    @ApiResource("edu:class:read")
    @PostMapping("/schedule")
    public Response<List<ClassPreScheduleResp>> schedule(@RequestBody @Validated ClassScheduleReq classScheduleReq) {
        return Response.success(lessonService.schedule(classScheduleReq));
    }

    @Operation(summary = "重新排课")
    @ApiResource("edu:class:write")
    @PostMapping("/reschedule/{classId}")
    public Response<Void> reschedule(@RequestBody @Validated ClassScheduleReq classScheduleReq, @PathVariable String classId) {
        lessonService.reschedule(classScheduleReq, classId);
        return Response.success();
    }
}
